package com.demo.controller;

import com.demo.dto.ClassesDTO;
import com.demo.dto.ClassesPageQueryDTO;
import com.demo.pojo.Classes;
import com.demo.result.PageResult;
import com.demo.result.Result;
import com.demo.service.ClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin(origins = "http://localhost:8080")
@RequestMapping("/instructor/classes")
public class ClassesController {
    @Autowired
    ClassesService classesService;

    @PostMapping("/page")
    public Result<PageResult> page(@RequestBody ClassesPageQueryDTO classesPageQueryDTO){
        PageResult pageResult=classesService.pageQuery(classesPageQueryDTO);
        System.out.println(pageResult);
        return Result.success(pageResult);
    }

    @PostMapping()
    public Result save(@RequestBody ClassesDTO classesDTO){
        classesService.save(classesDTO);
        return Result.success();
    }

    /**
     * 批量删除
     * @param classIDs
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestBody String[] classIDs){
        System.out.println(classIDs);
        classesService.delete(classIDs);
        return Result.success();
    }

    /**
     * 单个删除
     * @param classID
     * @return
     */
    @DeleteMapping("/id")
    public Result deleteById(@RequestParam("classID") String classID){
        classesService.deleteById(classID);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody ClassesDTO classesDTO){
        System.out.println(classesDTO);
        classesService.update(classesDTO);
        return Result.success();
    }
}
